package com.yh.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yh.bean.Goods;
import com.yh.service.GoodsService;


//商品控制器层
@Controller
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
	//商品展示列表
	@RequestMapping("listGoods")

	public List<Goods> listGoods(int gid){
		//ModelAndView mav=new ModelAndView();
		Goods goods=new Goods();
		goods.setGid(gid);

		List<Goods> listGoods=goodsService.listGoods(goods);




		//mav.addObject("listGoods", listGoods);
		//mav.setViewName("../index");
//		for (Goods goods : listGoods) {
//			System.out.println("======="+goods.getGname()+"=======");
//		}
		System.out.println("===================进入listGoods");
		return listGoods;
	}
	
	//商品加入订单时商品数量减1
	@RequestMapping("updateGoodsCount")
	@ResponseBody
	public Goods updateGoodsCount(int gid){
		Goods goods=new Goods();
		goods.setGid(gid);
		goodsService.updateGoodsCount(goods);
		System.out.println("=========="+goods.getGid()+"==============");
		return goods;
	}
	
	//商品数量为0时删除该商品
	@RequestMapping("deleteGoods")
	@ResponseBody
	public Goods deleteGoods(int gid){
		Goods goods=new Goods();
		goods.setGid(gid);
		System.out.println("==========指定商品ID="+goods.getGid()+"==============");
		goodsService.deleteGoods(goods);
		return goods;
	}
	
	//模糊查询所有商品
	@RequestMapping("fuzzyQuery")
	@ResponseBody
	public List<Goods> fuzzyQuery(String seaval){
		System.out.println("============用户输入的内容是============"+seaval);
		Goods goods=new Goods();
		goods.setGoodsAllClass(seaval);
		List<Goods> listGoods=goodsService.fuzzyQuery(goods);
		return listGoods;
	}
}
